<?php


class Users_adminController extends Controller {
    protected $allowed = array('administrator');

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        if ($this->isAllowed()) {
            $users = Model::loadRowsFromQuery("SELECT * FROM user;", "AuthModel");
            $this->render("admin/users/index" , array('users' => $users));
        }
        else {
            $this->showError('You are not allowed to open this page');
        }
    }

    public function inactivate($user_id) {
        if ($this->isAllowed()) {
            $user = Model::factory('auth' , $user_id);
            if ($user->loaded() && $this->isAllowed()) {
                $user->enabled = false;
                $user->save();
            }
            $this->redirect($this->createUrl('users_admin/index'));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function activate($user_id) {
        if ($this->isAllowed()) {
            $user = Model::factory('auth' , $user_id);
            if ($user->loaded() && $this->isAllowed()) {
                $user->enabled = true;
                $user->save();
            }
            $this->redirect($this->createUrl('users_admin/index'));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function reset($user_id) {
        if ($this->isAllowed()) {
            $user = Model::factory('auth' , $user_id);
            if ($user->loaded() && $this->isAllowed()) {
                $user->setPassword($user->username);
                $user->save();
            }
            $this->redirect($this->createUrl('users_admin/index'));
        }
        else
            $this->showError('You are not allowed to open this page');
    }
}


?>
